<?php

class krt_tNG_update extends krt_tNG {
	
	/**
		Constructor - get a reference to Kart Recordset
	*/
	function krt_tNG_update($connection) {
		parent::krt_tNG($connection);
		$this->type = '_update';
	}
	
	/**
		This function is executed after the BEFORE triggers and just before the real SQL transaction
		We don't have a real SQL transaction so we will use this function to perform our Kart IU tasks
	*/
	function prepareSQL() {
			foreach($this->fields as $key=>$value) {
				$this->updateKartFields($key,$value);
			}

		$this->mxKart->UpdateKart();
	}
	
	/**
		update kart fields for a specific fieldName
		@params
			$fieldName - field Name
			$values - associative array - 
						uid=>value
						values for every uid
		@return
			none
	*/
	function updateKartFields($fieldName,$values) {
		foreach($values as $key=>$value) {
			//if we try to update with quantity = 0 delete the entry
			$idx = $this->mxKart->getIndexForUID($key);
			if (($fieldName == $this->mxKart->quantityCol) && ($value == 0)) {
				if(is_numeric($value)) {
					$this->mxKart->deleteFields($idx);
				}
			} else {
				if ($idx != -1) {
					$value = abs((int)$value);
					$this->mxKart->updateOne($idx,$fieldName,$value);
				}
			}
		}
	}
}

?>
